package Leetcode;

public class Leetcode_30 {
        public static int bestRotation(int[] nums) {
            int n = nums.length;
            int[] diffs = new int[n];
            for (int i = 0; i < n; i++) {
                int low = (i + 1) % n;
                int high = (i - nums[i] + n + 1) % n;
                diffs[low]++;
                diffs[high]--;
                if (low >= high) {
                    diffs[0]++;
                }
            }
            int bestIndex = 0;
            int maxScore = 0;
            int score = 0;
            for (int i = 0; i < n; i++) {
                score += diffs[i];
                if (score > maxScore) {
                    bestIndex = i;
                    maxScore = score;
                }
            }
            return bestIndex;
        }
    public static void main(String[] arr){
        int[] num={2,3,1,4,0};
        System.out.println(bestRotation(num));
    }
}
